<?php
require_once ('../../src/db/connectdatabase.php');
require_once ("../dao/ProductDAO.php");
require_once('../../src/control/RedirectForward.php');

$url = 'admin/admin.php?c=product';

if(isset($_POST['act'])) {
	$action = $_POST['act'];
	$mess = "error";
	if($action == 'add') {
		if(isset($_POST['form_namepro']) && isset($_POST['form_content_tt']) && isset($_POST['form_content_pro']) && isset($_POST['link_images'])) {
			$id = time();
			$nameProduct = trim($_POST['form_namepro']);
			$contentTT = trim($_POST['form_content_tt']);
			$content = trim($_POST['form_content_pro']);
			$link = trim($_POST['link_images']);
			
			$url .= '&p=add-product';
			if($nameProduct != "" && $contentTT != "" && $content != "") {
				if(insertProduct($id, $nameProduct, $contentTT, $content, $link)) {
					$mess = "success";
				}
			}
		}
	} else if($action == 'edit') {
		if(isset($_POST['idpro']) && isset($_POST['form_namepro']) && isset($_POST['form_content_tt']) && isset($_POST['form_content_pro']) && isset($_POST['link_images'])) {
			$id = $_POST['idpro'];
			$nameProduct = trim($_POST['form_namepro']);
			$contentTT = trim($_POST['form_content_tt']);
			$content = trim($_POST['form_content_pro']);
			$link = trim($_POST['link_images']);
			
			$url .= '&p=edit-product&id='.$id;
			if($id != "" && $nameProduct != "" && $contentTT != "" && $content != "") {
				if(updateProduct($id, $nameProduct, $contentTT, $content, $link)) {
					$mess = "success";
					//$_REQUEST["username"] = "Tom";
				}
			}
		}
	} else if($action == 'clock') {// khoa khong cho phep hien thi hang loat
		if(isset($_POST['idcheckmanager'])) {
			$ids = $_POST['idcheckmanager'];
			if(count($ids) > 0 && displayProductList($ids, '0')) {
				$mess = 'success';
			}
		}
	} else if($action == 'unclock') {// cho phep hien thi hang loat
		if(isset($_POST['idcheckmanager'])) {
			$ids = $_POST['idcheckmanager'];
			if(count($ids) > 0 && displayProductList($ids, '1')) {
				$mess = 'success';
			}
		}
	} else if($action == 'del') {// xoa hang loat
		if(isset($_POST['idcheckmanager'])) {
			$ids = $_POST['idcheckmanager'];
			if(count($ids) > 0 && deleteListProduct($ids)) {
				$mess = 'success';
			}
		}
	}
	$url .= '&mess='.$mess;
}

redirect($url);
?>